package com.gomes.demo;



import com.gomes.functions.MultiFunction;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.function.*;

/**
 * <p>this class</P>
 *
 * @author gomes
 * @date 2023/10/8 8:50
 */
public class FunctionDemo {

    public static final String DEFAULT = "yyyy-MM-dd";

    public static void main(String[] args) {

        Integer then = square.andThen(plus).apply(2);

        Integer compose = plus.compose(square).apply(2);
        System.out.println("compose = " + compose);

        System.out.println("then = " + then);

        print.accept("hello");

        System.out.println(plusToString.apply(1, 2));

        System.out.println(getLastMonth.get());

        System.out.println(parse.apply("2021-12-09"));

        System.out.println(multi.apply(6, 6, 6));

        initMap.apply("very", "good").forEach((k, v) -> System.out.println(k + "->" + v));

        numToString.apply(1);
        System.out.println(numToString.apply(1L));
        numToString.apply(new BigDecimal("00"));


        System.out.println(moreThan2000.and(lessThan5000).test(3000));


        inf.apply(10);
    }

    /**
     * 计算 x²
     */
    static Function<Integer, Integer> square = arg -> arg * arg;


    static Function<Integer, Integer> plus = x -> x + x;

    /**
     * 两个参数一个返回值
     */
    static BiFunction<Integer, Integer, String> plusToString = (a, b) -> String.valueOf(a + b);


    /**
     * 解析日期字符串
     * 入参格式 ${DEFAULT}
     */
    static Function<String, LocalDate> parse = date -> LocalDate.parse(date, DateTimeFormatter.ofPattern(DEFAULT));


    /**
     * 有参无返回
     */
    static Consumer<String> print = x -> System.out.println(x.concat("!"));

    /**
     * 无参有返回
     * 获得上个月时间
     */
    static Supplier<String> getLastMonth = () -> LocalDateTime.now().minusMonths(1).format(DateTimeFormatter.ofPattern(DEFAULT));


    /**
     * 两个参数一个返回值
     */
    static BiFunction<String, String, Map<String, String>> initMap = Map::of;


    static Function<? super Number, String> numToString = String::valueOf;

    /**
     * 三个参数一个返回值
     */
    static MultiFunction<Integer, Integer, Integer, Integer> multi = (a, b, c) -> a * b * c;


    static Predicate<Integer> moreThan2000 = num -> num > 2000;

    static Predicate<Integer> lessThan5000 = num -> num < 5000;

    static IntFunction<BigDecimal> inf = BigDecimal::new;



}
